#include <stdio.h>

#include <stdlib.h>
#include <math.h>

#include <string.h>

//CMAX means max cylinder
#define CMAX 199

void printArr(int* arr, int length) {
	for (int i = 0; i < length; ++i) {
		printf("%d ", arr[i]);
	}
	printf("\n");
}

void FCFS(int* input, int inputLength, int* output, int* outputLength) {
	
	for (int i = 0; i < inputLength; ++i) {
		output[i] = input[i];
	}
	*outputLength = inputLength;
	printf("FCFS: ");
	printArr(output, *outputLength);
}

void SSTF(int* input, int inputLength, int* output, int* outputLength) {
	
	int indexArr[20] = { 0 };

	int headNum = input[0];
	int headIndex = 0;
	output[0] = headNum;
	indexArr[0] = 1;
	for (int i = 1; i < inputLength; ++i) {
		int min_index = 0;
		int minNum = RAND_MAX;
		
		for (int j = 0; j < inputLength; ++j) {
			if (headIndex== j) continue;
			if (indexArr[j] == 1) continue;
			if (abs(input[j] - headNum) < minNum) {
				min_index = j;
				minNum = abs(input[j] - headNum);
			}
			
		}
		indexArr[min_index] = 1;
		headNum = input[min_index];
		headIndex = min_index;
		output[i] = input[min_index];
	
	}
	*outputLength = inputLength;
	printf("SSTF: ");
	printArr(output, *outputLength);
}
void SCAN(int* input, int inputLength, int* output, int* outputLength) {

	int headNum = input[0];
	int headIndex = 0;
	*outputLength = 0;
	for (int i = 0; i < inputLength; i++)
	{
		for (int j = i + 1; j < inputLength; ++j) {
			if (input[i] > input[j]) {
				int temp = input[i];
				input[i] = input[j];
				input[j] = temp;
				if (i == headIndex) headIndex = j;
				else if (j == headIndex) headIndex = i;
			}
		}
	}
	
	for (int j = headIndex; j >= 0; --j) {
		output[(*outputLength)++] = input[j];
			
	}

	output[(*outputLength)++] = 0;

	for (int j = headIndex + 1; j < inputLength; ++j) {
		output[(*outputLength)++] = input[j];
		}

	


	printf("SCAN: ");
	printArr(output, *outputLength);

}
void LOOK(int* input, int inputLength, int* output, int* outputLength) {

	int headNum = input[0];
	int headIndex = 0;
	*outputLength = 0;
	for (int i = 0; i < inputLength; i++)
	{
		for (int j = i + 1; j < inputLength; ++j) {
			if (input[i] > input[j]) {
				int temp = input[i];
				input[i] = input[j];
				input[j] = temp;
				if (i == headIndex) headIndex = j;
				else if (j == headIndex) headIndex = i;
			}
		}
	}

	for (int j = headIndex; j >= 0; --j) {
		output[(*outputLength)++] = input[j];

	}

	

	for (int j = headIndex + 1; j < inputLength; ++j) {
		output[(*outputLength)++] = input[j];
	}




	printf("LOOK: ");
	printArr(output, *outputLength);

}
void CSCAN(int* input, int inputLength, int* output, int* outputLength) {
	int headNum = input[0];
	int headIndex = 0;
	*outputLength = 0;
	for (int i = 0; i < inputLength; i++)
	{
		for (int j = i + 1; j < inputLength; ++j) {
			if (input[i] > input[j]) {
				int temp = input[i];
				input[i] = input[j];
				input[j] = temp;
				if (i == headIndex) headIndex = j;
				else if (j == headIndex) headIndex = i;
			}
		}
	}

	for (int j = headIndex; j < inputLength; ++j) {
		output[(*outputLength)++] = input[j];

	}

	output[(*outputLength)++] = CMAX;
	output[(*outputLength)++] = 0;

	for (int j = 0; j < headIndex; ++j) {
		output[(*outputLength)++] = input[j];
	}




	printf("CSCAN: ");
	printArr(output, *outputLength);
}

void CLOOK(int* input, int inputLength, int* output, int* outputLength) {
	int headNum = input[0];
	int headIndex = 0;
	*outputLength = 0;
	for (int i = 0; i < inputLength; i++)
	{
		for (int j = i + 1; j < inputLength; ++j) {
			if (input[i] > input[j]) {
				int temp = input[i];
				input[i] = input[j];
				input[j] = temp;
				if (i == headIndex) headIndex = j;
				else if (j == headIndex) headIndex = i;
			}
		}
	}

	for (int j = headIndex; j < inputLength; ++j) {
		output[(*outputLength)++] = input[j];

	}



	for (int j = 0; j < headIndex; ++j) {
		output[(*outputLength)++] = input[j];
	}




	printf("CLOOK: ");
	printArr(output, *outputLength);
}


int main(int argc, char const* argv[])
{

	//initial head is the first item
	int input[] = { 53,98, 183, 37, 122, 14, 124, 65, 67 };

	int output[20];
	int output_length = 0;


	printf("input: ");
	printArr(input, 9);
	
	FCFS(input, 9, output, &output_length);
	//SSTF(input, 9, output, &output_length);
	//SCAN(input, 9, output, &output_length);
	//LOOK(input, 9, output, &output_length);
	//CSCAN(input, 9, output, &output_length);
	//CLOOK(input, 9, output, &output_length);
	

	return 0;
}
